package com.sl.hx.shop.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;

import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoImpl;
import com.jph.takephoto.model.InvokeParam;
import com.jph.takephoto.model.TContextWrap;
import com.jph.takephoto.model.TResult;
import com.jph.takephoto.permission.InvokeListener;
import com.jph.takephoto.permission.PermissionManager;
import com.jph.takephoto.permission.TakePhotoInvocationHandler;
import com.sl.hx.shop.R;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import butterknife.ButterKnife;

/**
 * @Author hwg
 * @Description：可以选择本地图片的activity基类
 * @Date 2018/1/17
 */
public abstract class BaseTakePhotoActivity<V, T extends BasePresenter<V>> extends RxAppCompatActivity
        implements BaseView, TakePhoto.TakeResultListener, InvokeListener {

    protected Context mContext ;

    protected T mPresenter ;

    private TakePhoto mTakePhoto;
    private InvokeParam mInvokeParam;

    /**
     * 绑定布局
     * @return
     */
    protected abstract @LayoutRes
    int getLayoutId();

    /**
     * 创建Presenter
     * @return
     */
    protected abstract T createPresenter();

    protected abstract void init();

    @Override
    public <T> LifecycleTransformer<T> bindToLife(ActivityEvent activityEvent) {
        return this.<T>bindUntilEvent(ActivityEvent.DESTROY);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.NoBackgroundTheme);
        getTakePhoto().onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);

        mContext = this;

        /**
         * 允许为空，不是所有页面都是需要实现mvp模式的
         * 不为空时，为view创建一个弱引用
         */
        if (createPresenter() != null){
            mPresenter = createPresenter() ;
            mPresenter.attachView((V) this);
        }

        setContentView(getLayoutId());

        //ButterKnife绑定布局
        ButterKnife.bind(this) ;

        init();

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        getTakePhoto().onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        getTakePhoto().onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //清空presenter对view的引用
        if (mPresenter != null){
            mPresenter.detachView();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //以下代码为处理Android6.0、7.0动态权限所需
        PermissionManager.TPermissionType type = PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.handlePermissionsResult(this, type, mInvokeParam, this);
    }

    /**
     * 获取TakePhoto实例
     *
     * @return
     */
    public TakePhoto getTakePhoto() {
        if (mTakePhoto == null) {
            mTakePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this, this));
        }
        return mTakePhoto;
    }

    @Override
    public void takeSuccess(TResult result) {

    }

    @Override
    public void takeFail(TResult result, String msg) {

    }

    @Override
    public void takeCancel() {

    }

    @Override
    public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
        PermissionManager.TPermissionType type = PermissionManager.checkPermission(TContextWrap.of(this), invokeParam.getMethod());
        if (PermissionManager.TPermissionType.WAIT.equals(type)) {
            this.mInvokeParam = invokeParam;
        }
        return type;
    }

    /**
     * 不带参数，从Activity跳转到指定Activity
     *
     * @param activity
     */
    protected void toActivity(Class<?> activity){
        startActivity(new Intent(this , activity));
    }
}
